package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.cty.ctyms.timejob.common.ConfigEnum;

public class UserInfoDAO {

	private IBaseDAO baseDAO;
	
	private ITransactionDAO tranDAO;
	
	/**
	 * @return the baseDAO
	 */
	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	/**
	 * @param baseDAO the baseDAO to set
	 */
	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	/**
	 * @return the tranDAO
	 */
	public ITransactionDAO getTranDAO() {
		return tranDAO;
	}

	/**
	 * @param tranDAO the tranDAO to set
	 */
	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}

	/**
	 * 查询直属机构下所有定制指定类型短信或邮件的人员
	 * @param DBCode
	 * @param MadeType 指定类型，如保养邮件、保养短信
	 * @return
	 */
	public List<Map<String,Object>> getZSUserInfo(String DBCode,String MadeType){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		try{
			String sql = "select ui.UserID,ui.UserName,ui.Mobile,ui.Email " +
					"from SMS_Alarm_SetInfo as smsas " +
					"Left Join UserInfo as ui ON smsas.SMSAS_UsP_ID=ui.UserID " +
					"Left Join V_UserInfo_Organization as uio ON uio.UserID=ui.UserID " +
					"Left Join Organization_DB as odb ON uio.OrganizationID=odb.OrganizationID " +
					"where 1=1 ";
				   String[] DBCodes = DBCode.split(",");
				   for(int i=0;i<DBCodes.length;i++){
					   if(i==0){
						   sql = " and odb.OrganizationCode = " + DBCodes[i];
					   }else{
						   sql = " or odb.OrganizationCode = " + DBCodes[i];
					   }
				   }
				   sql += " and smsas."+MadeType+"=1";
			list = this.baseDAO.execQuery(sql, new Object[]{});
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 查询机构下定制指定类型短信或邮件的人员
	 * 说明：机构检索方式：从直属机构向上检索。检索到人员后，则不再向上级检索。
	 * @param DBCode 直属组织机构Code
	 * @param MadeType 指定类型，如保养邮件、保养短信
	 * @return
	 */
	public List<Map<String,Object>> getUserInfo(String DBCode,String MadeType){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		try{
			if(StringUtil.isNullOrEmpty(DBCode)
					&&StringUtil.isNullOrEmpty(MadeType)){
				while(true){
					list = getZSUserInfo(DBCode,MadeType);
					if(list.size()>0)break;
					//获得最后分隔符的索引
					int index = DBCode.lastIndexOf(ConfigEnum.orgCodeCompart.getName());
					//如果得到的索引小于0（已经不存在分隔符），退出循环
					if(index<0)break;
					//获得父级Code值
					DBCode = DBCode.substring(0, index);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
	
	public List<Map<String,Object>> getUserInfo(String MadeType,String DBCode,String VehicleID){
		String sql = "select distinct SMSAS_ID,ui.UserID,ui.UserName,ui.Mobile,ui.Email,ui.organizationcode as UserDBCode,ui.organizationname as UserDBName,smsas.SMSAS_SMSAtt " 
			+"from SMS_Alarm_SetInfo as smsas "
//			+"inner Join UserInfo as ui ON smsas.SMSAS_UsP_ID=ui.UserID "
			+"inner Join v_userInfo_Organization_all as ui ON smsas.SMSAS_UsP_ID=ui.UserID " 
			+"left join SMSDB on smsas.SMSAS_ID=SMSDB_SMSAS_ID   "
			+"left join SMSVcl on smsas.SMSAS_ID=SMSVcl_SMSAS_ID "
			+"where ((smsas.SMSAS_SMSAtt=1 and SMSDB_DBCode=? ) or (smsas.SMSAS_SMSAtt=2 and SMSVcl_VehicleID=? )) and smsas."+MadeType+"=1";
		return this.baseDAO.execQuery(sql, new Object[]{DBCode,VehicleID});
	}
}
